from django.http import HttpResponseForbidden, HttpResponseRedirect

from django_sso.business import login_user, logout_user


def login(request, settings, next_page=None, required=False):
    destination_page, login_error = login_user(request, settings, next_page, required)
    if login_error:
        error = "<h1>Forbidden</h1><p>Login failed.</p>"
        return HttpResponseForbidden(error)
    else:
        HttpResponseRedirect(destination_page)


def logout(request, settings, next_page=None):
    destination_page = logout_user(request, settings, next_page)
    return HttpResponseRedirect(destination_page)
